package com.dupengcheng.subject.domain.hanler.subject;

import com.dupengcheng.subject.common.enums.IsDeleteEnum;
import com.dupengcheng.subject.common.enums.SubjectTypeEnum;
import com.dupengcheng.subject.domain.convert.SubjectAnswerConvert;
import com.dupengcheng.subject.domain.entity.SubjectAnswerBO;
import com.dupengcheng.subject.domain.entity.SubjectInfoBO;
import com.dupengcheng.subject.domain.entity.SubjectOptionBO;
import com.dupengcheng.subject.infra.basic.entity.SubjectRadio;
import com.dupengcheng.subject.infra.basic.service.SubjectRadioService;
import lombok.Getter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class RadioHandler implements SubjectTypeHandler {


    @Getter
    private SubjectTypeEnum subjectTypeEnum=SubjectTypeEnum.RADIO;

    @Resource
    private SubjectRadioService radioService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //先添加INfO表、再添加Mapping表、最后加单选表。
        List<SubjectRadio> subjectRadioList=new LinkedList<>();
        for (SubjectAnswerBO subjectAnswerBO : subjectInfoBO.getOptionList()) {
            SubjectRadio subjectRadio=new SubjectRadio();
            subjectRadio.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
            subjectRadio.setOptionContent(subjectAnswerBO.getOptionContent());
            subjectRadio.setIsCorrect(subjectAnswerBO.getIsCorrect());
            subjectRadio.setOptionType(subjectAnswerBO.getOptionType());
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadioList.add(subjectRadio);
        }

        radioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectRadio subjectRadio=new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        subjectRadio.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
        List<SubjectRadio> subjectRadioList=radioService.queryByCondtion(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOS=SubjectAnswerConvert.INSTANCE
                .convertRadioToAnswerList(subjectRadioList);
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }


}
